package cn.ffcs.uec.staff.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.outStaff.vo.UecOutStaffInfomationVo;
import cn.ffcs.uec.staff.dao.UecWorkDao;
import cn.ffcs.uec.staff.model.UecWork;
import cn.ffcs.uec.staffAndOutStaff.vo.StaffAndOutStaff;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.dao.BaseDaoImpl;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;

@Repository("uecWorkDao")
@Transactional
public class UecWorkDaoImpl extends BaseDaoImpl implements UecWorkDao {

	@Override
	public PageInfo queryPageInfoByUecWork(UecWork uecWork, int current,
			int pageSize) {
		if (null != uecWork) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_WORK T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);

			//判断这个对象是外包人员的数据还是合同制的数据
            if (BaseUnitConstants.NM_OUT_STAFF_TYPE.equals(uecWork.getNmStaffType())) {
                sb.append(" AND T.NM_STAFF_TYPE = ? ");
                params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
            }
            
			if (null != uecWork.getStaffId()) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecWork.getStaffId());
			} else if (null != uecWork.getPkPsndoc()) {
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(uecWork.getPkPsndoc());
			}
			
			return this.jdbcFindPageInfo(sb.toString(), params, current,
					pageSize, UecWork.class);
		}
		return null;
	}

	@Override
	public List<UecWork> queryPageInfoByUecStaffId(UecOutStaffInfo uecOutStaffInfo) {
		if (null != uecOutStaffInfo) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_WORK T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
                sb.append(" AND T.NM_STAFF_TYPE = ? ");
                params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecOutStaffInfo.getOutStaffId());
				sb.append(" order by to_date(T.begindate,'yyyy-mm-dd') asc");
			return this.jdbcFindList(sb.toString(), params, UecWork.class);
		}
		return null;
	}

	@Override
	public List<UecWork> queryPageInfoByUecOutAndStaffId(UecOutStaffInfomationVo uecOutStaffInfomationVo) {
		if (null != uecOutStaffInfomationVo) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_WORK T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			if(!StrUtil.isNullOrEmpty(uecOutStaffInfomationVo.getOutStaffId()) && uecOutStaffInfomationVo.getOutStaffId()>0) {
				sb.append(" AND T.NM_STAFF_TYPE = ? ");
                params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecOutStaffInfomationVo.getOutStaffId());
				sb.append(" order by to_date(T.begindate,'yyyy-mm-dd') asc");
			}
			/*if(!StrUtil.isNullOrEmpty(uecOutStaffInfomationVo.getStaffId())) {
				sb.append(" AND T.NM_STAFF_TYPE IS NULL ");
				sb.append(" AND T.pk_psndoc = ? ");
				params.add(uecOutStaffInfomationVo.getStaffId());
				sb.append(" order by to_date(T.begindate,'yyyy-mm-dd') asc");
			}*/
                
			return this.jdbcFindList(sb.toString(), params, UecWork.class);
		}
		return null;
	}

	@Override
	public List<UecWork> findUecWorkByPkPsndoc(StaffAndOutStaff staffAndOutStaff) {
		if (null != staffAndOutStaff) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_WORK T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			if(!StrUtil.isNullOrEmpty(staffAndOutStaff.getPkPsndoc())) {
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(staffAndOutStaff.getPkPsndoc());
				sb.append(" order by to_date(T.begindate,'yyyy-mm-dd') asc");
			}
                
			return this.jdbcFindList(sb.toString(), params, UecWork.class);
		}
		return null;
	}

}
